/*
 * 类名称:BenefitServiceImpl.java
 * 包名称:com.unimall.graded.app.api.benefit
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-03-17 09:48:29        lin     初版做成
 *
 * Copyright (c) 2019-2019 浵浵科技
 */
package com.unimall.graded.app.api.benefit;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.unimall.graded.data.domain.BenefitDO;
import com.unimall.graded.data.mapper.BenefitMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 分润记录表ServiceImpl实现类
 *
 * @author lin
 * @date 2020-03-17 09:48:29
 */
@Service
public class BenefitServiceImpl implements BenefitService {

    @Autowired
    private BenefitMapper benefitMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addBenefit(BenefitDO benefitDO) throws ServiceException {
        if (benefitDO == null) {
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
        if (benefitDO.getOrderId() == null) {
            throw new AdminServiceException("分润订单id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getLinkId() == null) {
            throw new AdminServiceException("推广链接id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getCustomerId() == null) {
            throw new AdminServiceException("直系下属id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getSellCustomerId() == null) {
            throw new AdminServiceException("销售人员id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getBenefitLevels() == null) {
            throw new AdminServiceException("分润总级别不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getBenefitLevel() == null) {
            throw new AdminServiceException("分润级别不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getCustomerBenefit() == null) {
            throw new AdminServiceException("分润金额，单位分不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getCurrentAmount() == null) {
            throw new AdminServiceException("当前进行分润的总金额不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getRechargeMoney() == null) {
            throw new AdminServiceException("分润金额不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getStatus() == null) {
            throw new AdminServiceException("账号状态[1:完成,2:未完成退款,3:已完成退款]不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }

        Date now = new Date();
        benefitDO.setId(null);
        benefitDO.setGmtCreate(now);
        benefitDO.setGmtUpdate(now);
        return benefitMapper.insert(benefitDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteBenefit(Long userId, Long id) throws ServiceException {
        return benefitMapper.delete(new EntityWrapper<BenefitDO>()
                .eq("id", id)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateBenefit(Long userId, BenefitDO benefitDO) throws ServiceException {
        if (benefitDO == null) {
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
        if (benefitDO.getOrderId() == null) {
            throw new AdminServiceException("分润订单id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getLinkId() == null) {
            throw new AdminServiceException("推广链接id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getCustomerId() == null) {
            throw new AdminServiceException("直系下属id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getSellCustomerId() == null) {
            throw new AdminServiceException("销售人员id不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getBenefitLevels() == null) {
            throw new AdminServiceException("分润总级别不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getBenefitLevel() == null) {
            throw new AdminServiceException("分润级别不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getCustomerBenefit() == null) {
            throw new AdminServiceException("分润金额，单位分不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getCurrentAmount() == null) {
            throw new AdminServiceException("当前进行分润的总金额不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getRechargeMoney() == null) {
            throw new AdminServiceException("分润金额不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        if (benefitDO.getStatus() == null) {
            throw new AdminServiceException("账号状态[1:完成,2:未完成退款,3:已完成退款]不能为空", ExceptionDefinition.PARAM_IS_NULL_CODE);
        }

        Date now = new Date();
        benefitDO.setGmtUpdate(now);
        return benefitMapper.updateById(benefitDO) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public BenefitDO getBenefitById(Long userId, Long id) throws ServiceException {

        if (id == null) {
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }

        return benefitMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<BenefitDO> getBenefitList(
            Long id,
            Long orderId,
            Long linkId,
            Long customerId,
            Long userId
    ) throws ServiceException {

        Wrapper<BenefitDO> wrapper = new EntityWrapper<BenefitDO>();
        wrapper.orderBy("id", false);

        if (id != null) {
            wrapper.eq("id", id);
        }
        if (orderId != null) {
            wrapper.eq("order_id", orderId);
        }
        if (linkId != null) {
            wrapper.eq("link_id", linkId);
        }
        if (customerId != null) {
            wrapper.eq("customer_id", customerId);
        }

        List<BenefitDO> BenefitDOs = benefitMapper.selectList(wrapper);
        return BenefitDOs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<BenefitDO> getBenefitListWithPage(
            Long userId,
            Long id,
            Long orderId,
            Long linkId,
            Long customerId,
            Integer pageNo, Integer limit) throws ServiceException {

        Wrapper<BenefitDO> wrapper = new EntityWrapper<BenefitDO>();
        wrapper.orderBy("id", false);

        if (id != null) {
            wrapper.eq("id", id);
        }
        if (orderId != null) {
            wrapper.eq("order_id", orderId);
        }
        if (linkId != null) {
            wrapper.eq("link_id", linkId);
        }
        if (customerId != null) {
            wrapper.eq("customer_id", customerId);
        }

        List<BenefitDO> BenefitDOs = benefitMapper.selectPage(new RowBounds((pageNo - 1) * limit, limit), wrapper);
        Integer count = benefitMapper.selectCount(wrapper);
        return new Page<BenefitDO>(BenefitDOs, pageNo, limit, count);
    }
}